import { Component, ChangeDetectionStrategy, OnInit, ChangeDetectorRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { DomSanitizer } from '@angular/platform-browser';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { RouteService, EmitterService } from '@core';

@Component({
  selector: 'notifications-detail',
  templateUrl: './notific-detail.component.html',
  styleUrls: ['./notific-detail.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificationsInnerDetailComponent implements OnInit {
  i: any;
  params: any;
  constructor(
    route: ActivatedRoute,
    private routeStateSrv: RouteService,
    private router: Router,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef,
    private dom: DomSanitizer,
    public msg: NzMessageService,
    private emitSrv: EmitterService,
  ) {
    this.params = route.snapshot.params;
  }

  back() {
    this.router.navigateByUrl(`/notification/inner/${this.params.type}/${this.routeStateSrv.data ? this.routeStateSrv.data.index : 0}`, {
      state: { pi: this.routeStateSrv.pi },
    });
  }

  ngOnInit(): void {
    this.http.get(`/cf/admin/web-msg/getone/${this.params.id}`).subscribe((res: any) => {
      if (res.data) {
        res.content = this.dom.bypassSecurityTrustHtml(res.data.content);
        this.i = res.data;
        if (this.i.isRead === '0') {
          this.setRead();
        }
      }
      this.cdr.detectChanges();
    });
    
  }

  setRead() {
    this.http.post(`/cf/admin/web-msg/readed`, [this.params.id]).subscribe(res => {      
      this.emitSrv.sendMessage({ type: 'regetUnread' });
    });
  }
}
